package com.model
{
	import com.EffectTypes;
	import com.model.region.Region;
	import com.model.region.RegionConverter;
	import com.rubenswieringa.book.Page;
	import com.services.ActiveResourceClient;
	
	import flash.events.ProgressEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.ProgressBar;
	import mx.core.Application;

	public class InteractivePage extends Page
	{
		private var _pageId:Number;
				
		public var image:Image = null;
		private var _bitmapLoader:BitmapLoader = null;
		private var _progressBar:ProgressBar = null;
		
		private var _pageURL:String;
		
		private var _regions:ArrayCollection = new ArrayCollection();;
		private var regionsResource:ActiveResourceClient = null;
		

        
        public static var EFFECTS: Array = [{label:"Fade", data:EffectTypes.FADE},
            								{label:"Wipe Right", data:EffectTypes.WIPE_RIGHT}, 	
											{label:"Wipe Left", data:EffectTypes.WIPE_LEFT},
											{label:"Wipe Up", data:EffectTypes.WIPE_UP},
											{label:"Wipe Down", data:EffectTypes.WIPE_DOWN},
											{label:"Iris", data:EffectTypes.IRIS},
											{label:"Glow", data:EffectTypes.GLOW}];
				
		public function InteractivePage(bookId:Number, pageId:Number, linksPositions:String = null)
		{
			super();
			_pageId = pageId;
			setStyle('backgroundColor', 0x000000);
			image = new Image();
			createProgressBar();
			if(Application.application.useDemoData || bookId == -1) {
				_pageURL = 'assets/img/page.jpg';
			} else {
				//image.autoLoad = false;
				_pageURL = IslamicBook.BACKEND_URL + "books/" + bookId + "/pages/" + pageId + ".jpg";				 
			}
			addChild(image);	
		} 
		
		/**
		 * Load the contents of the page(Background image, triggerLinks, external, regionEffect)
		 * It will be called when adding this page to the book view stacks 
		 */		
		public function loadContents():void {
			if(_bitmapLoader) {
				return;	
			}
			createProgressBar();
			this.addChild(_progressBar);
			_bitmapLoader = new BitmapLoader(_pageURL, imageLoadedHandler, imageProgressHandler, imageTimeOutHandler);
			
			if(!regionsResource) {
				//define a RESTful resource manager for regions
				regionsResource = new ActiveResourceClient();
				regionsResource.attachToCollection(_regions, new RegionConverter(this), "regions", false);
				regionsResource.baseURL = IslamicBook.BACKEND_URL + "books/" + Model.getIntstance().currentBook.bookId + "/pages/" + _pageId + "/";
				regionsResource.refreshList(addRegions);				
			}else {
				addRegions();
			}
		}
		
		/**
		 * UnLoad the contents of the page(Background image, triggerLinks, external, regionEffect)
		 * It will be called when removing this page from the book view stacks
		 */		
		public function unloadContents():void {
			image.source = null;
			_bitmapLoader = null;
			removeRegions();
		}
		
		public function createRegion(regionId:Number = -1):Region {
			var region:Region = new Region(this);
			region.regionId = regionId;
			_regions.addItem(region);
			return region;
		}
		
		private function addRegions():void {
			for each (var region:Region in _regions) {
				if(region.displayObject)
					this.addChild(region.displayObject);
			}
		}
		
		private function removeRegions():void {
			for each (var region:Region in _regions) {
				if(this.contains(region.displayObject))
					this.removeChild(region.displayObject);
			}
		}
		private function createProgressBar():void {
			_progressBar = new ProgressBar();
			_progressBar.x = 80;
			_progressBar.y = 250;
			_progressBar.width = 210;
			_progressBar.mode = 'manual';
		}
		
		/////////////////////////////////// Loading Handlers ////////////////////////////////////
		
		private function imageLoadedHandler():void {
			// remove the loading progress bar 
			if(_progressBar && this.contains(_progressBar))
				this.removeChild(_progressBar);
			_progressBar = null;
			if(_bitmapLoader) {
				// may be the page is unloaded before the calling this function
				image.source = _bitmapLoader.bitmap;
			}		
		}
		
		private function imageProgressHandler(e:ProgressEvent):void {
			if(_progressBar)
				_progressBar.setProgress(e.bytesLoaded, e.bytesTotal);
		}
		
		private function imageTimeOutHandler():void {
			Alert.show("Time Out");
		}
		
		//////////////////////////////////// Getter/Setters //////////////////
		
		public function get regions():ArrayCollection {
			return _regions;
		}
	}
}